$(function () {
    layui.use('table', function () {
        let table = layui.table;
        table.render({
            elem: '#table',
            height: 450,
            url: '/json_user',
            page: true, //开启分页
            align: 'center',

            cols: [[ //表头
                {field:'checkBox',align: 'center',type:'checkbox',width:'4%'},
                {field: 'id', title: '序号', sort: true,  align: 'center',type: 'number',width: '6%'},
                {field: 'username', title: '用户名',  align: 'center',width:'7%' },
                {field: 'password', title: '密码',  sort: true, align: 'center',width: '10%'},
                {field: 'email', title: '邮箱',  align: 'center',width: '13%'},
                {field: 'address', title: '收货地址',  align: 'center',width: '13%'},
                {field: 'phone', title: '手机号',  sort: true, align: 'center',width: '9%'},
                {field: 'qq', title: 'QQ号', sort: true, align: 'center',width: '8%'},
                {field: 'wechat', title: '微信号',  align: 'center',width: '9%'},
                {field: 'registerTime', title: '注册时间',width: '10%'},
                { title:'操作' , align: 'center',width: '11%',templet:function (){
                        let button= "<button type=\"button\" class=\"layui-btn layui-btn-sm layui-btn-warm\" lay-event='edit'>编辑</button>";
                        button+="<button type=\"button\" class=\"layui-btn layui-btn-sm layui-btn-danger\" lay-event='del'>删除</button>";
                        return button;
                    }
                }
            ]],
            limit: 10,  //一页显示10条数据
            parseData: function (res) {
                let result;
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        });


        //搜索
        $("#searchInput").keydown(function(e) {
            //绑定回车
            if (e.keyCode == '13') {//keyCode=13是回车键
                $('#searchButton').click();
            }
        });
        $('#searchButton').click(function (){
            let content = $('#searchInput').val();
            table.reload('table',{
                url:"/searchAllUser",
                page: {curr:1},
                where:{
                    "content":content
                }
            })

        })
        table.on('tool(table)',function (obj){
            if(obj.event==='del'){
                layer.confirm('是否删除所选中行?', {
                    icon: 3,
                    title:'提示',
                }, function(index){
                    layer.msg('删除成功', {
                        time: 1000
                    });
                    $.get({
                        url: "deleteUser",
                        data: {
                            "id":obj.data.id
                        },
                        success:function (data){
                            if (data === 'success'){
                                layer.msg('删除成功', {
                                    time: 1000,
                                    end:function (){
                                        table.reload('table');
                                        layer.close(index);
                                    }
                                });
                            }
                            else{
                                layer.msg('删除失败', {
                                    time: 1000,
                                })
                            }
                        },
                        error:function (data){
                            layer.msg('删除失败', {
                                time: 1000,
                            })
                        }
                    });
                });
            }else if(obj.event==='edit'){
                layer.open({
                    type:2,
                    tittle:'编辑用户',
                    content:'/updateUser',
                    area:['600px','500px'],
                    offset:"15px",
                    closeBtn:2,
                    success:function (layero,index){
                        //数据回显
                        let body = layer.getChildFrame('body',index);
                        body.find('#id').val(obj.data.id);
                        body.find('#username').val(obj.data.username);
                        body.find('#password').val(obj.data.password);
                        body.find('#email').val(obj.data.email);
                        body.find('#address').val(obj.data.address);
                        body.find('#phone').val(obj.data.phone);
                        body.find('#qq').val(obj.data.qq);
                        body.find('#wechat').val(obj.data.wechat);
                        body.find('#registerTime').val(obj.data.registerTime);
                    }
                })
            }
        })
    });
})